package cn.school.prevention.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.school.common.utils.SysUserUtils;
import cn.school.prevention.dao.FeedBackDao;
import cn.school.prevention.entity.FeedBack;
import cn.school.prevention.entity.Scope;
import cn.school.prevention.service.FeedBackService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class FeedBackServiceImpl extends ServiceImpl<FeedBackDao, FeedBack> implements FeedBackService {

    @Autowired
    private FeedBackDao feedBackDao;

    @Override
    public IPage<FeedBack> FeedBackPageList(FeedBack feedBack) {
        LambdaQueryWrapper<FeedBack> wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByDesc(FeedBack::getId);
        Scope scope=new Scope();
        Long id = scope.getId();
        wrapper.eq(FeedBack::getScopeId,id);
        IPage<FeedBack> page = page(new Page<>(feedBack.getPageNum(), feedBack.getPageSize()), wrapper);
        return page;
    }

    @Override
    public int insert(FeedBack feedBack) {
        String userName = SysUserUtils.getSysUser().getUsername();
        feedBack.setCreateBy(userName);
        feedBack.setCreateTime(new Date());
        return feedBackDao.insert(feedBack);
    }

    @Override
    public int update(FeedBack feedBack) {
        String userName = SysUserUtils.getSysUser().getUsername();
        feedBack.setModifyBy(userName);
        feedBack.setModifyTime(new Date());
        return feedBackDao.updateById(feedBack);
    }
    
    @Override
    public int updateByStudent(FeedBack feedBack) {
        String userName = SysUserUtils.getStudentUser().getPhone();
        feedBack.setFeedbackTime(DateUtil.format(Calendar.getInstance().getTime(), "yyyy-MM-dd HH:mm:ss"));
        feedBack.setModifyBy(userName);
        feedBack.setModifyTime(new Date());
        return feedBackDao.updateById(feedBack);
    }

    @Override
    public List<FeedBack> list(FeedBack feedBack) {
        return feedBackDao.list(feedBack);
    }
}
